package com.example.androidproject.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class GroupRepository {
    private SQLiteDatabase database;
    private GroupDatabaseHelper dbHelper;

    public GroupRepository(Context context) {
        dbHelper = new GroupDatabaseHelper(context);
        database = dbHelper.getWritableDatabase();
        dbHelper.createTable(database);
    }

    public void insertGroup(String groupName, String username, String ip) {
        // 检查表中是否已经存在相同的组名
        if (!isGroupNameExists(groupName)) {
            ContentValues values = new ContentValues();
            values.put(GroupDatabaseHelper.COLUMN_GROUP_NAME, groupName);
            values.put(GroupDatabaseHelper.COLUMN_USERNAME, username);
            values.put(GroupDatabaseHelper.COLUMN_IP, ip);
            database.insert(GroupDatabaseHelper.TABLE_GROUPS, null, values);
        }
    }

    /**
     * 检查是否存在相同的组名
     * @param groupName 组名
     * @return 如果存在返回true，否则返回false
     */
    private boolean isGroupNameExists(String groupName) {
        String[] projection = { GroupDatabaseHelper.COLUMN_ID };
        String selection = GroupDatabaseHelper.COLUMN_GROUP_NAME + " = ?";
        String[] selectionArgs = { groupName };

        Cursor cursor = database.query(
                GroupDatabaseHelper.TABLE_GROUPS,
                projection,
                selection,
                selectionArgs,
                null,
                null,
                null
        );

        boolean exists = (cursor.getCount() > 0);
        cursor.close();
        return exists;
    }

    /**
     * 获取所有组记录
     * @return 包含组记录的列表
     */
    public List<GroupRecord> getAllGroups() {
        List<GroupRecord> groups = new ArrayList<>();
        Cursor cursor = database.query(
                GroupDatabaseHelper.TABLE_GROUPS,
                new String[]{GroupDatabaseHelper.COLUMN_ID, GroupDatabaseHelper.COLUMN_GROUP_NAME, GroupDatabaseHelper.COLUMN_USERNAME, GroupDatabaseHelper.COLUMN_IP},
                null, null, null, null, null
        );

        int idIndex = cursor.getColumnIndexOrThrow(GroupDatabaseHelper.COLUMN_ID);
        int groupNameIndex = cursor.getColumnIndexOrThrow(GroupDatabaseHelper.COLUMN_GROUP_NAME);
        int usernameIndex = cursor.getColumnIndexOrThrow(GroupDatabaseHelper.COLUMN_USERNAME);
        int ipIndex = cursor.getColumnIndexOrThrow(GroupDatabaseHelper.COLUMN_IP);

        if (cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(idIndex);
                String groupName = cursor.getString(groupNameIndex);
                String username = cursor.getString(usernameIndex);
                String ip = cursor.getString(ipIndex);
                groups.add(new GroupRecord(id, groupName, username, ip));
            } while (cursor.moveToNext());
        }
        cursor.close();
        return groups;
    }

    /**
     * 数据类，用于封装组记录
     */
    public static class GroupRecord {
        private int id;
        private String groupName;
        private String username;
        private String ip;

        public GroupRecord(int id, String groupName, String username, String ip) {
            this.id = id;
            this.groupName = groupName;
            this.username = username;
            this.ip = ip;
        }

        public GroupRecord(String groupName, String username, String ip) {
            this.groupName = groupName;
            this.username = username;
            this.ip = ip;
        }

        public int getId() {
            return id;
        }

        public String getGroupName() {
            return groupName;
        }

        public String getUsername() {
            return username;
        }

        public String getIp() {
            return ip;
        }

        @Override
        public String toString() {
            return "GroupRecord{" +
                    "id=" + id +
                    ", groupName='" + groupName + '\'' +
                    ", username='" + username + '\'' +
                    ", ip='" + ip + '\'' +
                    '}';
        }
    }
}
